package cn.source.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.system.domain.GkCardManage;
import cn.source.system.service.IGkCardManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.GkCard;
import cn.source.system.service.IGkCardService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 发卡信息Controller
 *
 * @author sourcebyte.vip
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/system/gkCard")
public class GkCardController extends BaseController
{
    @Autowired
    private IGkCardService gkCardService;

    @Autowired
    private IGkCardManageService gkCardManageService;

    /**
     * 查询发卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:gkCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(GkCard gkCard)
    {

        //获取登陆用户id
        Long userId=getUserId();
        GkCardManage cardManage=new GkCardManage();
        cardManage.setUserId(userId);
        List<GkCardManage> manageList=gkCardManageService.selectGkCardManageList(cardManage);
        if(manageList!=null&&manageList.size()>0){
            cardManage=manageList.get(0);
            gkCard.setStartCardNo(cardManage.getStartCardNo());
            gkCard.setEndCardNo(cardManage.getEndCardNo());
        }
        startPage();
        List<GkCard> list = gkCardService.selectGkCardList(gkCard);
        for (GkCard gkCard2 : list){
            if(gkCard2.getZt().intValue()==0){
                gkCard2.setZtName("未激活");
            }
            if(gkCard2.getZt().intValue()==1){
                gkCard2.setZtName("已激活");
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取发卡信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gkCardService.selectGkCardById(id));
    }

    /**
     * 新增发卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:gkCard:add')")
    @Log(title = "发卡信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GkCard gkCard)
    {
        return toAjax(gkCardService.insertGkCard(gkCard));
    }

    /**
     * 修改发卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:gkCard:edit')")
    @Log(title = "发卡信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GkCard gkCard)
    {
        return toAjax(gkCardService.updateGkCard(gkCard));
    }

    /**
     * 删除发卡信息
     */
    @PreAuthorize("@ss.hasPermi('system:gkCard:remove')")
    @Log(title = "发卡信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gkCardService.deleteGkCardByIds(ids));
    }

    /**
     * 导出发卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:gkCard:export')")
    @Log(title = "发卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GkCard gkCard)
    {
        List<GkCard> list = gkCardService.selectGkCardList(gkCard);
        ExcelUtil<GkCard> util = new ExcelUtil<GkCard>(GkCard.class);
        util.exportExcel(response, list, "发卡信息数据");
    }
}
